﻿using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Helpers;

namespace Coldairarrow.Api.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    public class UploadController : BaseController
    {
        readonly IConfiguration _configuration;
        public UploadController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [HttpPost]
        public IActionResult UploadFileByForm()
        {
            var file = Request.Form.Files.FirstOrDefault();
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());
            string fileName = Guid.NewGuid().ToString("N");
            string path = $"/Upload/{fileName}/{file.FileName}";
            string _path = $"\\{fileName}\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
                file.CopyTo(fs);
            }

            string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 上传PDA打包的apk
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadApkFileByForm()
        {
            var file = Request.Form.Files.FirstOrDefault();
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());
            string fileName = Guid.NewGuid().ToString("N");
            string path = $"/Upload/apk/{file.FileName}";
            string _path = $"\\apk\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
                file.CopyTo(fs);
            }

            string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };
            

            return JsonContent(res.ToJson());
        }

        /*
    * 开发人：崔楠
    * 提交日期：2022-02-18 15:08
    * 修改内容：编写minio操作类
    * 上传SVN：否
    */
        /// <summary>
        /// 插入minio
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> UploadFileToMinioAsync()
        {
            var file = Request.Form.Files.FirstOrDefault();
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());
            string fileName = Guid.NewGuid().ToString("N");
            string path = $"/Upload/{file.FileName}";
            string _path = $"\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
           
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
               
                    
                file.CopyTo(fs);
            }
            //初始化minio
            MinioHelper minio = new MinioHelper();
            var url = await minio.UploadFile("shengzhuo", physicPath);
            //string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };


            return JsonContent(res.ToJson());
        }
        /// <summary>
        /// 插入minio
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> UploadFileToMinioIMageAsync()
        {
            var file = Request.Form.Files.FirstOrDefault();
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());
            string fileName = Guid.NewGuid().ToString("N");
            string path = $"/Upload/{fileName}/{file.FileName}";
            string _path = $"\\{fileName}\\{file.FileName}";
             string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {


                file.CopyTo(fs);
            }
            //初始化minio
            MinioHelper minio = new MinioHelper();
            var url = await minio.UploadFile("shengzhuo", physicPath);
            //string url = $"{_configuration["WebRootUrl"]}{path}";
          string guid = Guid.NewGuid().ToString();
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };


            return JsonContent(res.ToJson());
        }






        /// <summary>
        /// 图片上传增加水印
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadFileToMinioIoAsync()
        {
            var file = Request.Form.Files.FirstOrDefault();
            byte[] bytearray = new byte[file.Length];
            var image = new WebImage(bytearray);
            image.AddTextWatermark("设置水印内容", "Blank",12, "Regular","Microsoft Sans Serif","Right","Bottom",100,5);
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());
            string fileName = file.FileName;//Guid.NewGuid().ToString("N");
            string path = $"/Upload/{file.FileName}";
            string _path = $"\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {


                file.CopyTo(fs);
            }
            //初始化minio
            MinioHelper minio = new MinioHelper();
            var url = await minio.UploadFile("shengzhuo", physicPath);
            image.Save(url);
            image.Clone();
            //string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };


            return JsonContent(res.ToJson());
        }
    }
}
